package database;


import java.math.BigDecimal;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class Sql {

    public static int generateID(String tabela, String coloana) {
        int id = 1;
        BigDecimal idMax = null;
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;

        try {
            con = DatabaseConnection.getConectare();
            System.out.println(con);
            stmt = con.createStatement();
            String sql = " select MAX(" + coloana + ")+1 ID from " + tabela;
            rs = stmt.executeQuery(sql);
            if (rs.next()) {
                idMax = rs.getBigDecimal("ID");
            }
            if (idMax != null) {
                id = idMax.intValue();
            }
      
        } catch (ClassNotFoundException ex) {

        } catch (SQLException ex) {
            System.out.println("err sql" + ex);

        }

        finally {
            try {
                if (rs != null)
                    rs.close();
            } catch (SQLException ex) {

            }
            try {
                if (stmt != null)
                    stmt.close();
            } catch (SQLException ex) {

            }

        }
        return id;
    }


}
